<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <div class="text item">
      <el-form ref="form" label-width="100px" :model="userPassword" :rules="formRules">
        <el-form-item label="原密码" prop="old_pwd">
          <el-input v-model="userPassword.old_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="userPassword.new_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="re_pwd">
          <el-input v-model="userPassword.re_pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatePwd">提交修改</el-button>
          <el-button @click="$refs.form.resetFields()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { updatePwdAPI } from '@/APl/user.js'
export default {
  data() {
    var validatePass3 = (rule, value, callback) => {
      if (value !== '') {
        if (this.userPassword.new_pwd !== '') {
          // callback(new Error('新旧密码不能一致!'))
          this.$refs.form.validateField('new_pwd')
        }

        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value !== '') {
        if (this.userPassword.re_pwd !== '') {
          this.$refs.form.validateField('re_pwd')
        } else if (value === this.userPassword.old_pwd) {
          callback(new Error('新旧密码不能一致!'))
        }

        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.userPassword.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userPassword: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      formRules: {
        old_pwd: [
          { required: true, message: '原密码不能为空!', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码为6-15位非空字符串组成', trigger: 'blur' },
          { validator: validatePass3, trigger: ['blur', 'change'] }
        ],
        new_pwd: [
          { required: true, message: '新密码不能为空!', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码为6-15位非空字符串组成', trigger: 'blur' },
          { validator: validatePass, trigger: ['blur', 'change'] }
        ],
        re_pwd: [
          { required: true, message: '请确认新密码!', trigger: 'blur' },
          { validator: validatePass2, trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    updatePwd() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const { data: res } = await updatePwdAPI(this.userPassword)
          if (res.code === 0) {
            this.$message.success(res.message)
            this.$refs.form.resetFields()
            this.$store.commit('user/updateToken', '')
            this.$router.push('/login')
          } else {
            this.$message.error(res.message)
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  font-size: 13px;
}
.el-form {
  width: 500px;
}
</style>
